package com.example.share.controller;

import com.example.share.mapper.UserMapper;
import com.example.share.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class loginController {

    /*首页*/
    /*@RequestMapping("/")
    public String index(){
        return "index";
    }*/

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/log")
    public String login(String username, String password, Model model, HttpSession session){
//        User user = userMapper.selectById(1);
        HashMap<String, Object> map = new HashMap<>();
        //自定义要查询的
        map.put("user_name",username);
        List<User> users = userMapper.selectByMap(map);

        //具体的业务
        if (password.equals(users.get(0).getUserPassWord())){
            return "success";
        }else {
            //告诉用户登录失败
//            model.addAttribute("msg","用户名或者密码错误！");
            return "index";
        }
    }

    /*注册跳转*/
    @RequestMapping("/res")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(String username,String password,String userTel, String userNickName){

        User user = new User();
        user.setUserName(username);
        user.setUserPassWord(password);
        user.setUserTel(userTel);
        user.setUserNickName(userNickName);
        user.setUserContributionDegree(1);
        user.setUserStatus(1);
        user.setUserIsDeleted(0);
        userMapper.insert(user);

        return "index";
    }
}
